package com.hockeo.server.servlet;

import java.util.Locale;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * Standard locale resolver for the VH application. The locale to use is read from the
 * HTTP accept-language header. If the requested language is supported, the resolved
 * locale will use the specified language. Otherwise, the default locale is used as
 * fallback.
 * </p>
 *
 * <p>
 * The {@link Locale} that is returned by the {@link #resolveLocale(HttpServletRequest)}
 * method always uses the country of the default locale. Only the language is changed.
 * Example: If the default locale is 'de-CH' and the client that connects uses 'fr-FR',
 * the resolved locale will be 'fr-CH' (if French is an accepted language, see
 * {@link #setAcceptedLanguages(Set)} for details).
 * </p>
 *
 * @version $Id: VHLocaleResolver.java 40 2006-10-08 15:36:13Z janjanke $
 * @author jjanke
 */
public class LocaleResolver
{
  /*
   * ================================================================================
   * Private members
   * ================================================================================
   */
  private Map<String, Locale> d_mapAcceptedLanguages;

  /*
   * ================================================================================
   * Non-static methods
   * ================================================================================
   */

  /**
   * Sets the accepted languages. This LocaleResolver only accepts locales that refer to
   * an accepted language. If a request refers to a non-accepted language, the default
   * language will automatically be used.
   *
   * @param setLanguages a set containing the officile ISO two letter abbreviations for
   *          all languages that are supported by the application
   */
  public void setAcceptedLanguages( Map<String, Locale> mapAcceptedLanguages )
  {
    d_mapAcceptedLanguages = mapAcceptedLanguages;
  }

  /**
   * TODO
   *
   * @return
   */
  public Locale getDefaultLocale()
  {
    return null; // TODO
  }

  /**
   * TODO
   *
   * @param rq
   * @return
   */
  public Locale resolveLocale( HttpServletRequest rq )
  {
    Locale locale = d_mapAcceptedLanguages.get( rq.getLocale().getLanguage() );

    if ( locale == null )
      locale = getDefaultLocale();

    return locale;
  }

  /**
   * Resolves the locale from the given locale string.
   *
   * @param strLocale the locale string (e.g. 'de', 'de_CH' or 'fr_FR_UNIX' etc.)
   * @return the matching locale object
   */
  public Locale resolveLocale( final String strLocale )
  {
    String strRequestedLanguage;

    if ( strLocale.equals( "default" ) )
      return getDefaultLocale();

    if ( strLocale.length() == 2 )
      strRequestedLanguage = strLocale;
    else
      strRequestedLanguage = strLocale.substring( 0, strLocale.indexOf( '_' ) );

    Locale locale = d_mapAcceptedLanguages.get( strRequestedLanguage );

    if ( locale == null )
      locale = getDefaultLocale();

    return locale;
  }
}
